package com.qf.dao;/*
 * @author     : XXX
 * @date       : 2022/5/27 13:57
 * @description: some description
 */

import com.qf.pojo.User;
import com.qf.utils.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    //创建dbUtils工具
    private QueryRunner queryRunner = new QueryRunner(DBUtil.getDateSource());

    @Override
    public User findByUserName(String userName) {
        String sql = "select * from user where username=?";
        try {
            User user = queryRunner.query(sql, new BeanHandler<User>(User.class), userName);
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int addUser(User user) {
        String sql = "insert into user (uid, username, password, status) value(?, ?, ?, ?)";
        try {

            int count = queryRunner.update(sql,user.getUid(),user.getUsername(),user.getPassword(),user.getStatus());
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }


}
